package controller;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;
import tool.JsonResult;
import tool.Pager;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller//控制器注解
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("getpageusers")
    public String getpageusers(User user, Pager pager,Model model){
        int count = userService.getcount(user);
        List<User> users = userService.getPageUsers(user,
                (pager.getCurrentPage() - 1) * pager.getPageSize(),pager.getPageSize());
        pager.setTotal(count);
        pager.setPageRecords(users);
        model.addAttribute("pager",pager);
        model.addAttribute("searchuser",user);
        return "userindex";
    }

    @RequestMapping("useradd")
    @ResponseBody
    public JsonResult useradd(User user){
        System.out.println("douseradd");

        System.out.println(user);
        Integer result = userService.adduser(user);
        System.out.println(result);
        if (result>0){
            return new JsonResult(true, 10000, "添加成功", null);
        }else {
            return new JsonResult(false, 10001, "添加失败", null);

        }
    }

    @RequestMapping("del/{userid}")
    @ResponseBody
    public JsonResult userdel(@PathVariable Integer userid){
        System.out.println("delid"+userid);

        Integer result = userService.deluser(userid);
        System.out.println(result);
        if (result>0){
            return new JsonResult(true, 10000, "删除成功", null);
        }else {
            return new JsonResult(false, 10001, "删除失败", null);

        }
    }

    @RequestMapping("update/{userid}")
    public String userupdate(@PathVariable Integer userid,HttpSession session) {
        System.out.println("updateid" + userid);

        User updateuser = userService.getUserById(userid);
        System.out.println(updateuser);

        if (updateuser!=null) {
            session.setAttribute("updateuser",updateuser);
            return "userupdate";
        } else {
            throw new RuntimeException();

        }

    }

    @RequestMapping("userupdate")
    @ResponseBody
    public JsonResult douserupdate(User user){
        System.out.println("doupdate");
        System.out.println(user);

        Integer result = userService.updateuser(user);
        System.out.println(result);
        if (result>0){
            return new JsonResult(true, 10000, "修改成功", null);
        }else {
            return new JsonResult(false, 10001, "修改失败", null);

        }
    }


    @RequestMapping("topUp")
    @ResponseBody
    public JsonResult topUp(Integer userid,Double addmoney){
        System.out.println("topUp"+userid);
        System.out.println(addmoney);

        User user = userService.getUserById(userid);
        if (user==null){
            return new JsonResult(false, 10001, "充值失败 用户不存在", null);
        }


        Integer result = userService.topUp(userid,addmoney);
        System.out.println(result);
        if (result>0){
            return new JsonResult(true, 10000, "充值成功", null);
        }else {
            return new JsonResult(false, 10001, "充值失败", null);

        }
    }
}
